import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:zootix/constants/zt_global_url.dart';
import 'package:zootix/services/zt_app_info.service.dart';
import 'package:zootix/services/zt_dio.service.dart';
import 'package:zootix/services/zt_storage.service.dart';
import 'package:zootix/zt_config.dart';

import '../constants/zt_constants.dart';

Future<Response> login(String? oauthType, String? token) {
  return ZtDioService.dio.post(ZtUrls.login,
      data: {'oauthType': oauthType, 'token': token},
      options: Options(contentType: "application/x-www-form-urlencoded"));
}

Future<Response> logout() {
  return ZtDioService.dio.post(
    ZtUrls.logout,
  );
}

Future<Response> deleteAccount() {
  return ZtDioService.dio.post(
    ZtUrls.deleteAccount,
  );
}

Future<Response> getAppConfig() {
  Map<String, dynamic> query = {'ver': 0};
  return ZtDioService.dio.get(ZtUrls.getAppConfig, queryParameters: query);
}

Future<Response> getOssPolicy() {
  return ZtDioService.dio.get(ZtUrls.getOssPolicy);
}

Future<Response> isValidToken(String token) {
  return ZtDioService.dio.post(ZtUrls.isValidToken, data: {'token': token});
}

Future<Response> getRankList() {
  Map<String, dynamic> data = {
    "category": "Popular",
    "isPageMode": true,
    "isRemoteImageUrl": true,
    "limit": 20,
    "page": 1,
    "tag": "All"
  };
  return ZtDioService.dio.post(ZtUrls.rankList, data: data);
}

Future<Response> addFriend(String userId) {
  Map<String, dynamic> data = {
    "followUserId": userId,
  };
  return ZtDioService.dio.post(ZtUrls.addFriend, data: data);
}

Future<Response> removeFriend(String userId) {
  Map<String, dynamic> data = {
    "followUserId": userId,
  };
  return ZtDioService.dio.post(ZtUrls.removeFriend, data: data);
}

Future<Response> getGoodsList() {
  Map<String, dynamic> data = {
    "isIncludeSubscription": false,
    "payChannel": "IAP"
  };
  return ZtDioService.dio.post(ZtUrls.goodsList, data: data);
}

Future<Response> saveUserInfo(Map<String, dynamic> data) {
  return ZtDioService.dio.post(ZtUrls.saveUserInfo, data: data);
}

Future<Response> getUserInfo(String userId) {
  Map<String, dynamic> data = {
    "userId": userId,
  };
  return ZtDioService.dio.get(ZtUrls.getUserInfo, queryParameters: data);
}

Future<Response> uploadImg(File file) async {
  DateTime dateTime = DateTime.now();
  String fileName = file.path.split('/').last;
  String format = fileName.split('.').last;
  int imageTimeName = dateTime.millisecondsSinceEpoch +
      (dateTime.microsecondsSinceEpoch ~/ 1000000);
  String imageName = '$imageTimeName.$format';
  String host = ZtAppInfoService.to.ossPolicy.host!;
  String dir = ZtAppInfoService.to.ossPolicy.dir!;
  final filePath = await MultipartFile.fromFile(file.path, filename: fileName);
  final formData = FormData.fromMap({
    'ossaccessKeyId': ZtAppInfoService.to.ossPolicy.accessKeyId,
    'policy': ZtAppInfoService.to.ossPolicy.policy,
    'signature': ZtAppInfoService.to.ossPolicy.signature,
    'callback': ZtAppInfoService.to.ossPolicy.callback,
    'key': '$dir/$imageName',
    'file': filePath,
  });
  return ZtDioService.dio.post(host, data: formData);
}

Future<Response> updateAvatar(String filename) {
  return ZtDioService.dio
      .post(ZtUrls.updateAvatar, data: {'avatarPath': filename});
}

Future<Response> getFollowedList(int page, int limit) {
  Map<String, dynamic> data = {"limit": limit, "page": page};
  return ZtDioService.dio.post(ZtUrls.getFollowedList, data: data);
}

// Block / Report
// Porngraphic, False gender, Fraud, Politicl sensitive, Other
Future<Response> reportComplain(
    int broadcasterId, String complainCategory, String? complainSub) {
  return ZtDioService.dio.post(ZtUrls.reportComplain, data: {
    'broadcasterId': broadcasterId,
    'complainCategory': complainCategory,
    'complainSub': complainSub ?? ''
  });
}

Future<Response> unblock(int blockUserId) {
  return ZtDioService.dio.post(ZtUrls.unblock, data: {
    'blockUserId': blockUserId,
  });
}

Future<Response> getBlockList() {
  return ZtDioService.dio.post(ZtUrls.blockList);
}

Future<Response> googleTranslate(String content, String language) {
  Map<String, String> data = {
    "target": language,
    "q": content,
    "format": 'text'
  };
  return ZtDioService.dio.post(ZtUrls.googleTranslate,
      queryParameters: {
        "key": ZtAppInfoService.to
            .getConfigByCode<String>('google_translation_key'),
      },
      data: data);
}

Future<Response> getIMStrategy() {
  return ZtDioService.dio.get(
    ZtUrls.getIMStrategy,
  );
}

Future<Response> getIMToken() {
  return ZtDioService.dio.get(
    ZtUrls.getIMToken,
  );
}

Future<Response> createOrder({
  required String goodsCode,
}) {
  return ZtDioService.dio.post(ZtUrls.createOrder, data: {
    'entry': '',
    'goodsCode': goodsCode,
    'payChannel': ZtGloablConfig.PAY_CHANNEL,
    'source': ''
  });
}

Future<Response> validateOrder(
    {required String orderNo,
    required String payload,
    required String transactionId}) {
  return ZtDioService.dio.post(ZtUrls.validateOrder, data: {
    'orderNo': orderNo,
    'payload': payload,
    'transactionId': transactionId,
    'type': 1
  });
}

Future<Response> reviewModeConsume({
  required int num,
}) {
  return ZtDioService.dio.post(ZtUrls.reviewModeConsume, data: {
    'outlay': num,
    'source': 'iap',
  });
}
